var app =getApp()

// pages/loupan/comment/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
		id:'',
		index:'',
		detailss:'',
		wendadetail:'',
		hufulist:'',
		count:'',// 评论数
		plcontent:'', // 评论内容
		cid:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		console.log(options)
		var that = this;
		that.setData({
			id:options.id,
			index:options.index,
			cid:options.cid
		})
		that.detail()
  },
	detail() {
		var id = this.data.id;
		var cid =this.data.cid;
		var that = this;
		var opid = wx.getStorageSync('user').openid;
		var index = that.data.index
		app.core('get', 'get_houses_detail/id/' + id + '/open_id/' + opid + '', {}
			, (res) => {
				console.log(res)
			
				var x = res.data.loupan_wenda;
				var arr=[];

				var wenda = '';
				for (var i = 0; i < x.length; i++) {
					if (x[i].id == cid) {
						wenda =x[i]
					}
				}

				for(var i =0;i<x.length;i++){
					if (x[i].p_wenda_id == cid){
				
						arr.push(x[i])
					}
				}
				var count = arr.length;

				console.log(arr,'回复liebiao')
				that.setData({
					wendadetail: wenda,
					detailss: res.data,
					hufulist:arr,
					count: count
				})
			}
		)
	},// 获取详情
  /**
   * 生命周期函数--监听页面初次渲染完成
   */

	getcontent(e){
		var that = this
		var val = e.detail.value;
		that.setData({
			plcontent: val
		});
	},// 获取评论输入框值

	commt(){
		var that =this;
		var content = that.data.plcontent;
    var opid = app.globalData.openid;
		// var opid ='oe_-o5daoHoRkihnEmUouT2moarU';
		var loupanid = that.data.id;
		var p_wenda_id = that.data.cid;
		var p_xiaochengxu_user_id = that.data.wendadetail.p_xiaochengxu_user_id;
		
		if(content == ''){
			wx.showToast({
				title: '请输入评论内容',
				icon: 'none',
				duration: 1000
			})
		}else{
			app.core('post','create_loupan_wenda',{
				loupan_id:loupanid,
				p_xiaochengxu_user_id: p_xiaochengxu_user_id,
				content:content,
				p_wenda_id: p_wenda_id,
				open_id: opid
			},(res=>{
				console.log(res)
				
					if (res.resultCode == 200){
						wx.showToast({
							title: '评论成功',
							icon: 'none',
							duration: 1000
						})
            that.setData({
              plcontent:''
            })
						// wx.reLaunch({
						// 	url: 'pages/loupan/ershoudetail?id='+loupanid+'',
						// })
					setTimeout(function(){
						var options={
							id: that.data.id,
							index: that.data.index,
							cid: that.data.cid
						}
						that.onLoad(options)
					},1000)
					}
			}))
		}

	},

  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})